Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change X509CertificateLoader to wrap unsupported algorithms with a CryptographicException #104040

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

vcsjones
Copy link
Member

Before the X509CertificateLoader was merged, the ReadECDsaPrivateKey_BrainpoolP160r1_Pfx test on Android was passing, but for the wrong reason. The test is asserting that the platform throws a CryptographicException for brainpool curves. On Android it was throwing a CryptographicException because the PFX is protected with RC2, which is unavailable on Android.

This PR changes the PBE decryption to wrap the PlatformNotSupportedException with a CryptographicException with the same message, restoring the previous behavior.

We should probably investigate fixing the brainpool test to use an algorithm other than RC2 so we can confirm the Android tests fail for the right reason, but this unblocks CI.

Before loader merge:

   Exception messages: System.Security.Cryptography.CryptographicException : The certificate data cannot be read with the provided password, the password may be incorrect.
---- System.PlatformNotSupportedException : Algorithm 'RC2' is not supported on this platform.   Exception stack traces:    at System.Security.Cryptography.X509Certificates.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
   at System.Security.Cryptography.X509Certificates.AndroidCertificatePal.ReadPkcs12(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean ephemeralSpecified)
   at System.Security.Cryptography.X509Certificates.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean readingFromFile, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.CertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)

After this pull request:

   Exception messages: System.Security.Cryptography.CryptographicException : The certificate data cannot be read with the provided password, the password may be incorrect.
---- System.PlatformNotSupportedException : Algorithm 'RC2' is not supported on this platform.   Exception stack traces:    at System.Security.Cryptography.X509Certificates.X509CertificateLoader.ThrowWithHResult(String message, Int32 hResult, Exception innerException)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.BagState.DecryptSafeContents(AlgorithmIdentifierAsn& algorithmIdentifier, ReadOnlySpan`1 passwordSpan, ReadOnlySpan`1 encryptedContent)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.DecryptSafeContents(ContentInfoAsn safeContentsAsn, Pkcs12LoaderLimits loaderLimits, ReadOnlySpan`1 passwordSpan, BagState& bagState, Nullable`1& workRemaining)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.ReadCertsAndKeys(BagState& bagState, ReadOnlyMemory`1 data, ReadOnlySpan`1& password, Pkcs12LoaderLimits loaderLimits)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(ReadOnlyMemory`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12Pal(ReadOnlySpan`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at System.Security.Cryptography.X509Certificates.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean readingFromFile, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.CertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)

Fixes #104030

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's unblock the CI ;)

@stephentoub
Copy link
Member

This PR changes the PBE decryption to wrap the PlatformNotSupportedException with a CryptographicException with the same message, restoring the previous behavior.

The test is actually checking the exception message? We generally try not to do that.

@vcsjones
Copy link
Member Author

The test is actually checking the exception message? We generally try not to do that.

No, the test does not do that. When I say "same message" meant the product behavior. "Let's go back to throwing a CryptographicException like we were before the loader was merged, and the exception text that I used for the CryptographicException is also the same as it was before".

@bartonjs
Copy link
Member

The test is actually checking the exception message?

I don't think it is, since in the premise he points out that me regressing CryptographicException to PNSE here unmasked that we have a test that is passing, but for the wrong reason.

@bartonjs
Copy link
Member

/azp run runtime-android

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bartonjs bartonjs merged commit 2b4a4bc into main Jun 27, 2024
81 of 87 checks passed
@akoeplinger akoeplinger deleted the vcsjones/android-rc2 branch June 27, 2024 13:35
@github-actions github-actions bot locked and limited conversation to collaborators Jul 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android][arm] Algorithm 'RC2' not supported on Android causing failures in X509Certificates tests
4 participants